package com.lagou.mapper;

import com.lagou.pojo.User;
import org.apache.ibatis.annotations.*;

import java.io.IOException;
import java.util.List;

public interface UserMapper {

    User findById(int id) throws IOException;

    User findByCondition(User user);

    List<User> findByIds(Integer[] array);

    List<User> findAllUserAndOrders();

    @Select("select * from user order by id")
    List<User> findAll();

    List<User> findAllUserAndRole();

    // 多对多
    @Select("select * from user")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "roleList",column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.lagou.mapper.RoleMapper.findByUid"))
    })
    List<User> findAllUserToRole();

    // 一对多
    @Select("select * from user")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "orderList",column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.lagou.mapper.OrdersMapper.findByUid"))
    })
    List<User> findAllUserToOrders();

    @Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
    void add(User user);

    void update(User user);

    @Delete("delete from user where id=#{id}")
    void delete(int id);
}
